5.6 .a. Test and develop a wrapper method power and an underlying recursive method that return the result of integer exponentiation. The method specification of the wrapper method is
/**
* Calculates the value of a given integer raised to the power of a second integer.
* The worstTime(n) is O(n), where n is the second integer.
*
* @param i - the base integer (to be raised to a power).
* @param n - the exponent (the power i is to be raised to).
*
* @return the value of i to the nth power.
*
* @throws IllegalArgumentException - if n is a negative integer or if i raised to
* to the n is greater than Long.MAX_VALUE.
*
*/
public static long power (long i, int n)
Hint: We define 00 = 1, so for any integer i , i 0 = 1. For any integers i> 0 and n > 0,
i n = i∗i n−1
b. Develop an iterative version of the power method.
c. Develop an underlying recursive version called by the power method for which worstTime(n) is logarithmic in n.
Hint: If n is even, power (i, n) = power (i * i, n/2); if n is odd, power (i, n) = i *
in - 1 = i * power (i * i, n/2).
For testing parts b and c, use the same test suite you developed for part a.
 
 
View Solution
 
 
 
<< Back Next >>